#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <BEntry>
#include <BIcon>
#include <BImage>
#include <BButton>
#include <BLineEdit>
#include <BTextEdit>
#include <BTreeWidget>
#include <BTabWidget>
#include <BFileDialog>
#include <BMenuBar>
#include <BMenu>
#include <BXmlNode>
#include <BSonNode>
#include <BMainWindow>

#include "OperaStackDialog.h"
#include "SearchDialog.h"
#include "FontDialog.h"

class MainWindow : public BMainWindow
{
public:
	MainWindow();
	~MainWindow();

	bool openFile(const BString& fileName);

protected:
	void initMenuBar();
	bool loadConfig(const BString& fileName);
	BTextFormat* createFormat(const char* ext);
	BWidget* tabOfFile(const BString& fileName);
	void signChanged(BWidget* widget, bool changed);
	BTreeItem* createTreeItem(BEntry* entry);
	BTreeItem* createTreeItem(BSonNode* node);
	bool openTreeItem(BTreeItem* item);
	void freshItemIcon(BTreeItem* item);
	void writeSonNode(BTreeItem* item, BSonNode* node);
	void updateToolBar();

protected:
	void slotEditFile(BObject* object, const BValue& value);

	void slotWorkspaceTitleChanged(BObject* object, const BValue& value);
	void slotWorkspaceStyled(BObject* object, const BValue& value);
	void slotTreeItemRightClicked(BObject* object, const BValue& value);
	void slotTreeItemDoubleClicked(BObject* object, const BValue& value);

	void slotTabCurrentChanged(BObject* object, const BValue& value);
	void slotEditorPropertyChanged(BObject* object, const BValue& value);
	void slotTabClicked(BObject* object, BValue& value);
	void slotTabRightClicked(BObject* object, BValue& value);

	void slotTabClosing(BObject* object, BValue& value);
	void slotRemoveButtonClicked(BObject* object, const BValue& value);
	void slotClearButtonClicked(BObject* object, const BValue& value);
	void slotStackListButtonToggled(BButton* button, const BValue& value);

	void slotOpenPathActionTriggered(BObject* object, const BValue& value);
	void slotOpenFileActionTriggered(BObject* object, const BValue& value);
	void slotNewFileActionTriggered(BObject* object, const BValue& value);
	void slotQuitActionTriggered(BObject* object, const BValue& value);
	void slotSaveAsActionTriggered(BObject* object, const BValue& value);
	void slotSaveAllActionTriggered();
	void slotSaveFileActionTriggered(BObject* object, const BValue& value);

	void slotUndoActionTriggered(BObject* object, const BValue& value);
	void slotRedoActionTriggered(BObject* object, const BValue& value);
	void slotSearchActionTriggered(BObject* object, const BValue& value);
	void slotCopyActionTriggered(BObject* object, const BValue& value);
	void slotCutActionTriggered(BObject* object, const BValue& value);
	void slotPasteActionTriggered(BObject* object, const BValue& value);
	void slotRemoveActionTriggered(BObject* object, const BValue& value);

	void slotPageMenuTrigger(BObject* object, const BValue& value);
	void slotTableMenuTrigger(BObject* object, const BValue& value);
	void slotTabMenuTrigger(BObject* object, const BValue& value);

	void slotTabCloseTrigger(BObject* object, const BValue& value);
	void slotSearchDialogHide(BObject* object, const BValue& value);
	void slotSearchFinished(BObject* object, const BValue& value);
	void slotResultItemDoubleClicked(BObject* object, const BValue& value);

	void slotMenuAddTrigger(BObject* object, const BValue& value);
	void slotMenuPrependTrigger(BObject* object, const BValue& value);
	void slotMenuAppendTrigger(BObject* object, const BValue& value);

	void slotShowPunctMenuTriggered(BMenu* menu, const BValue& value);
	void slotFontActionTriggered(BAction* action, const BValue& value);
	void slotToolMenuTriggered(BMenu* menu, const BValue& value);
	void slotStyleMenuTriggered(BMenu* menu, const BValue& value);
	void slotMenuStyled(BMenu* menu, const BValue& value);

	void slotTextEditFramed(BTextEdit* textEdit, const BValue& value);
	void slotTextEdited(BTextEdit* textEdit, const BValue& value);

protected:
	virtual void closeEvent(const BEvent& event);

	virtual void updateEvent(const BEvent& event);

	virtual void styleEvent(const BEvent& event);

private:
	BMenuBar*			_menuBar;
	BMenu*				_fileMenu;
	BMenu*				_editMenu;
	BMenu*				_viewMenu;
	BMenu*				_toolMenu;

	BButton*			_openPathButton;
	BButton*			_openFileButton;
	BButton*			_newFileButton;
	BButton*			_saveButton;
	BButton*			_saveAllButton;
	BButton*			_removeButton;
	BButton*			_clearButton;
	BButton*			_searchButton;
	BButton*			_undoButton;
	BButton*			_redoButton;
	BButton*			_stackListButton;
	BHolder<BWidget>	_stackListWidget;

	BTreeWidget*		_workspace;
	BTabWidget*			_tabWidget;
	BTreeWidget*		_searchBoard;

	SearchDialog*		_searchDialog;
	FontDialog*			_fontDialog;
	BFileDialog*		_fileDialog;
	BMenu*				_listMenu;
	BMenu*				_pageMenu;
	BMenu*				_tableMenu;
	BMenu*				_tabMenu;
	BMenu*				_useStyleMenu;

	BFolder				_includeFolder;
	BFolder				_documentFolder;

	BImageHolder		_normalImage;
	BImageHolder		_dirtyImage;
	BImageHolder		_emptyImage;

	BIconHolder			_htmlIcon;
	BIconHolder			_textIcon;
	BIconHolder			_treeIcon;
};

#endif